package com.piggy.logi.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.piggy.common.core.exception.GlobalException;
import com.piggy.common.core.utils.PageUtils;
import com.piggy.common.core.web.page.PagePlus;
import com.piggy.common.core.web.page.TableDataInfo;
import com.piggy.common.satoken.utils.SecurityUtils;
import com.piggy.logi.sys.bo.LogiWaybillLogEditBo;
import com.piggy.logi.sys.bo.LogiWaybillLogQueryBo;
import com.piggy.logi.sys.domain.LogiScheduleLog;
import com.piggy.logi.sys.domain.LogiWaybill;
import com.piggy.logi.sys.domain.LogiWaybillLog;
import com.piggy.logi.sys.enums.WaybillStatusEnum;
import com.piggy.logi.sys.mapper.LogiWaybillLogMapper;
import com.piggy.logi.sys.service.ILogiScheduleService;
import com.piggy.logi.sys.service.ILogiWaybillLogService;
import com.piggy.logi.sys.service.ILogiWaybillService;
import com.piggy.logi.sys.vo.LogiScheduleExtVo;
import com.piggy.logi.sys.vo.LogiWaybillLogVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * 运单运行日志Service业务层处理
 *
 * @author piggy
 * @date 2023-12-07
 */
@Slf4j
@Service
public class LogiWaybillLogServiceImpl extends ServiceImpl<LogiWaybillLogMapper, LogiWaybillLog> implements ILogiWaybillLogService {
     private final static String TEMPLATE = "运单[%s]已经[%s]";

    @Resource
    private ILogiWaybillService waybillService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addLog(long waybillId, int status) {
        LogiWaybill waybill = waybillService.getById(waybillId);
        if (waybill == null) {
            throw new GlobalException("运单不存在");
        }
        LogiWaybillLog waybillLog = new LogiWaybillLog();
        waybillLog.setDeptId(SecurityUtils.getMainDeptId());
        waybillLog.setWaybillId(waybillId);
        waybillLog.setOutCode(waybill.getOutCode());
        waybillLog.setLogMsg(String.format(TEMPLATE, waybill.getOutCode(), WaybillStatusEnum.get(status).getInfo()));
        waybillLog.setTimeLoad(new Date());
        waybillLog.setOperator(SecurityUtils.getUserId());
        waybillLog.setOperatePhone(SecurityUtils.getPhone());
        waybillLog.setFlowStatus(status);
        save(waybillLog);
    }

    @Override
    public List<LogiWaybillLogVo> waybillLogDetail(Long waybillId) {
        return queryList(new LogiWaybillLogQueryBo().setWaybillId(waybillId));
    }

    @Override
    public LogiWaybillLogVo queryById(Long id){
        return getVoById(id, LogiWaybillLogVo.class);
    }

    @Override
    public TableDataInfo<LogiWaybillLogVo> queryPageList(LogiWaybillLogQueryBo bo) {
        PagePlus<LogiWaybillLog, LogiWaybillLogVo> result = pageVo(PageUtils.buildPagePlus(false), buildQueryWrapper(bo), LogiWaybillLogVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<LogiWaybillLogVo> queryList(LogiWaybillLogQueryBo bo) {
        return listVo(buildQueryWrapper(bo), LogiWaybillLogVo.class);
    }

    private LambdaQueryWrapper<LogiWaybillLog> buildQueryWrapper(LogiWaybillLogQueryBo bo) {
        LambdaQueryWrapper<LogiWaybillLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, LogiWaybillLog::getId, bo.getId());
        lqw.eq(bo.getWaybillId() != null, LogiWaybillLog::getWaybillId, bo.getWaybillId());
        lqw.eq(StrUtil.isNotBlank(bo.getOutCode()), LogiWaybillLog::getOutCode, bo.getOutCode());
        lqw.eq(bo.getFlowStatus() != null, LogiWaybillLog::getFlowStatus, bo.getFlowStatus());
        lqw.eq(bo.getTimeLoad() != null, LogiWaybillLog::getTimeLoad, bo.getTimeLoad());
        lqw.eq(StrUtil.isNotBlank(bo.getLogMsg()), LogiWaybillLog::getLogMsg, bo.getLogMsg());
        lqw.eq(bo.getOperator() != null, LogiWaybillLog::getOperator, bo.getOperator());
        lqw.eq(StrUtil.isNotBlank(bo.getOperatePhone()), LogiWaybillLog::getOperatePhone, bo.getOperatePhone());
        lqw.orderByAsc(LogiWaybillLog::getFlowStatus, LogiWaybillLog::getTimeLoad);
        return lqw;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LogiWaybillLogVo insertByAddBo(LogiWaybillLogEditBo bo) {
        LogiWaybillLog add = BeanUtil.toBean(bo, LogiWaybillLog.class);
        save(add);
        return queryById(add.getId());
    }

    @Override
    @Transactional
    public LogiWaybillLogVo updateByEditBo(LogiWaybillLogEditBo bo) {
        LogiWaybillLog update = BeanUtil.toBean(bo, LogiWaybillLog.class);
        updateById(update);
        return queryById(update.getId());
    }

    @Override
    @Transactional
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
